<template>
  <div v-if="course" class="course-detail">
    <div class="course-header">
      <img :src="course.coverImage" :alt="course.title">
      <div class="course-info">
        <h1>{{ course.title }}</h1>
        <p>{{ course.description }}</p>
        <div class="course-meta">
          <span>讲师：{{ course.instructor.name }}</span>
          <span>难度：{{ getLevelText(course.level) }}</span>
          <span>学生数：{{ course.studentCount }}</span>
        </div>
      </div>
    </div>

    <div class="course-sections">
      <div class="course-chapters">
        <h2>课程章节</h2>
        <div v-for="chapter in chapters" :key="chapter.id" class="chapter">
          <h3>{{ chapter.title }}</h3>
          <ul>
            <li v-for="lesson in chapter.lessons" :key="lesson.id">
              {{ lesson.title }}
              <span>{{ lesson.duration }}分钟</span>
            </li>
          </ul>
        </div>
      </div>

      <div class="course-actions">
        <div class="price-section">
          <span class="price">¥ {{ course.price }}</span>
          <button @click="enrollCourse" :disabled="isEnrolled">
            {{ isEnrolled ? '已报名' : '立即报名' }}
          </button>
        </div>
      </div>
    </div>

    <div class="course-reviews">
      <h2>课程评价</h2>
      <div v-for="review in reviews" :key="review.id" class="review">
        <div class="review-header">
          <img :src="review.student.avatar" :alt="review.student.name">
          <span>{{ review.student.name }}</span>
          <div class="rating">{{ '★'.repeat(review.rating) }}</div>
        </div>
        <p>{{ review.comment }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { courseService } from '@/services/course.service'
import { Course, Chapter, CourseReview } from '@/types/course'

const props = defineProps({
  courseId: {
    type: String,
    required: true
  }
})

const course = ref<Course | null>(null)
const chapters = ref<Chapter[]>([])
const reviews = ref<CourseReview[]>([])
const isEnrolled = ref(false)

onMounted(async () => {
  try {
    course.value = await courseService.getCourseDetail(props.courseId)
    chapters.value = await courseService.getCourseChapters(props.courseId)
    reviews.value = await courseService.getCourseReviews(props.courseId)
    
    // 检查是否已报名
    // isEnrolled.value = await courseService.checkEnrollment(props.courseId)
  } catch (error) {
    console.error('获取课程详情失败', error)
  }
})

const getLevelText = (level: string) => {
  const levelMap = {
    'BEGINNER': '入门',
    'INTERMEDIATE': '中级',
    'ADVANCED': '高级'
  }
  return levelMap[level] || level
}

const enrollCourse = async () => {
  try {
    await courseService.enrollCourse(props.courseId)
    isEnrolled.value = true
  } catch (error) {
    console.error('报名失败', error)
  }
}
</script>

<style scoped>
.course-detail {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.course-header {
  display: flex;
  margin-bottom: 20px;
}

.course-header img {
  width: 300px;
  height: 200px;
  object-fit: cover;
  margin-right: 20px;
}

.course-meta {
  display: flex;
  gap: 15px;
  color: #666;
}

.course-sections {
  display: flex;
  justify-content: space-between;
}

.course-chapters {
  flex: 2;
}

.course-actions {
  flex: 1;
  margin-left: 20px;
}

.price-section {
  text-align: center;
}

.price {
  font-size: 1.5em;
  color: #f44336;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
}

.course-reviews .review {
  border-bottom: 1px solid #eee;
  padding: 10px 0;
}

.review-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.review-header img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.rating {
  color: #FFD700;
}
</style>